package dw2;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

public class DWGraphics {
    BufferedImage image;
    int image_width;
    Rectangle bounds;
    DWGraphics(Image tempimage)
    {
      //got idea from http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Supplements/Chapter11/bufferedImage.html
      image = new BufferedImage(tempimage.getWidth(null),tempimage.getHeight(null),BufferedImage.TYPE_INT_ARGB);
      Graphics2D graphictemp = image.createGraphics();
      graphictemp.drawImage(tempimage,0,0,null);
      image_width = image.getWidth();
      bounds = new Rectangle(0,0,tempimage.getWidth(null),tempimage.getHeight(null));
    }

    public int getWidth()
    {
        return image_width;
    }

    public int getHeight()
    {
        return image.getHeight();
    }

    public Rectangle getBounds()
    {
        return bounds;
    }
    public void draw(Graphics g,int x,int y,int direction,double rotate)
    {

        draw(g,x,y,direction,rotate,image);
    }

    //Creating a seperate draw function which will allow the Animation class to simply
    //pass in the part of the image they want to draw and let the DWGraphics class handle the rest
    protected void draw(Graphics g,int x,int y,int direction,double rotate,BufferedImage theimage)
    {

        Graphics2D g2d = (Graphics2D) g;
        AffineTransform trans = new AffineTransform();
        int coord_x,coord_y;

        //bounds.setLocation(direction, direction);

        if(direction == 1 )
        {
            
            coord_x = x+image_width;
            coord_y = y;
            trans.setToTranslation(coord_x,coord_y);
            trans.scale(-1.0,1.0);
            //Since it is flipping along the y axis need to shift the image to align correctly
            coord_x -= image_width;
        }
        else
        {
            coord_x = x;
            coord_y = y;
          
            trans.setToTranslation(x,y);
        }

    
        bounds.setLocation(coord_x,coord_y);
        //g2d.draw(bounds);
        trans.rotate(Math.toRadians(rotate),image_width/2,image.getHeight()/2);
        g2d.drawImage(theimage, trans,null);

    }


}
